<template>
  <el-dialog
    title="提示"
    :visible.sync="visible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model.number="form.mobile" maxlength="11"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="form.timeOfEntry"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择聘用形式">
          <el-option label="正式" :value="1"></el-option>
          <el-option label="非正式" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-cascader
          v-model="form.departmentName"
          :options="options"
          :props="{ label: 'name', value: 'name' }"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          type="date"
          placeholder="选择日期"
          v-model="form.correctionTime"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="onConfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDepartmentApi } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import { addUserApi } from '@/api/employees'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: [],
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职日期', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正日期', trigger: 'blur' }
        ]
      },
      options: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 初始化部门列表数据
    async initData() {
      const { depts } = await getDepartmentApi()
      this.options = tranListToTreeData(depts, '')
    },
    // 处理弹框关闭
    handleClose() {
      this.form = {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: [],
        correctionTime: ''
      }
      this.$emit('update:visible', false)
    },
    // 处理新增接口调用逻辑
    async onConfirm() {
      await this.$refs.form.validate()
      const cloneForm = JSON.parse(JSON.stringify(this.form))
      cloneForm.departmentName =
        cloneForm.departmentName[cloneForm.departmentName.length - 1]
      await addUserApi(cloneForm)
      this.$message.success('新增成功')
      this.handleClose()
      this.$emit('success')
    }
  }
}
</script>

<style scoped>
.el-input,
.el-select,
.el-date-editor,
.el-cascader {
  width: 500px !important;
}
</style>
